import html from '../../assets/partials/ad-list.html';
import './ad-edit';

$.confAd = function confAd(groupId) {
    var dialog = $.confirm({
        title: "确定是否下发广告配置",
        message: "此次下发会下发该分组及子分组底下的所有服务器",
        confirm: function() {
            let body = dialog.getModalBody()[0];
            $.myAjax({
                url: "ad/confAd.do",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    groupId: groupId
                }),
                success: function(result) {
                    var table = $(".table").DataTable();
                    table.draw();
                }
            });
        }
    });
}

$.delAd = function delAd(id) {
    let ids = [];
    if (id == undefined) {
        let $checkbox = $("tbody").find("input[type='checkbox']");
        $checkbox.each(function(index, item) {
            if ($(this).is(':checked')) {
                ids.push($(this).val());
            }
        });
    } else {
        ids.push(id);
    }
    if (ids.length == 0) {
        alert("请至少选择一组广告");
        return;
    }
    $.confirm({
        title: '删除广告',
        message: "确定是否删除广告？",
        confirm: function() {
            $.myAjax({
                url: "ad/delAd.do",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    ids: ids
                }),
                success: function(res) {
                    var table = $(".table").DataTable();
                    table.draw();
                }
            });
        }
    });
}

export default {
    name: 'ad',
    title: '广告管理',
    html: html,
    beforeMount: element => {
        console.info('马上要显示了');
    },
    mounted: (element, menudata) => {
        console.info('已经显示出来');
        element.find('table').dataTable({
            "dom": 'rt<"clearfix table-bottom"ilp>',
            "searching": false,
            "ordering": false,
            "serverSide": true,
            "processing": true,
            "language": {
                'emptyTable': '当前暂无数据',
                'info': '第 _PAGE_ 页 / 总 _PAGES_ 页，',
                'infoEmpty': '',
                'lengthMenu': '每页 _MENU_ ',
                "paginate": {
                    "first": "第一页",
                    "last": "最后一页",
                    "next": "下一页",
                    "previous": "上一页"
                }
            },
            "pagingType": "full_numbers",
            "columns": [{
                    "data": "groupName"
                },
                {
                    "render": function(data, type, row) {
                        var className = "label-success";
                        switch (row.status) {
                            case "CONF_FAIL":
                                className = "label-danger";
                                break;
                            case "RUNNING":
                                className = "label-primary";
                                break;
                            case "NOT_SYNC":
                                className = "label-default";
                                break;
                        }
                        return '<span class="label ' + className + ' radius">' + row.statusDesc + '</span>';
                    }
                },
                {
                    "data": "lastConfTime"
                },
                {
                    "data": "createTime"
                },
                {
                    "data": "updateTime"
                },
                {
                    "render": function(data, type, row) {
                        var $div = $("<div class='table-btn'>");
                        $div.append($("<i class='icon icon-download' title='下发配置' onclick='$.confAd(" + row.groupId + ")'>"));
                        $div.append($("<i class='icon icon-inlineEdit' title='修改广告' onclick='$.addAd(" + row.groupId + ")'>"));
                        $div.append($("<i class='icon icon-del' title='删除广告' onclick='$.delAd(" + row.groupId + ")'>"));
                        return $div.html();
                    }
                }
            ],
            "ajax": function(data, callback, settings) {
                var pageSize = data.length;
                var page = data.start / pageSize + 1;
                data.groupId = $('#tree-panel').find(".selected").attr("data-id");
                data.pageSize = pageSize;
                data.page = page;
                $.myAjax({
                    url: "ad/getAdList.do",
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function(result) {
                        result.recordsTotal = result.total;
                        result.recordsFiltered = result.total;
                        return callback(result);
                    }
                });
            }
        });

    },
    unmounted: element => {
        console.info('不显示了');
    }
};
